import * as ActionTypes from '../constants/actionTypes'
import api from '../utils/api'
import qs from 'qs'

const searchKeys = [
  'id',
]

export const fetchItems = (params = {}, accessToken) => async (dispatch) => {
  dispatch({ type: ActionTypes.TRANSACTIONLIST_FETCHITEMS_LOAD })
  try {
    const res = await fetch(`http://localhost:3000/ocap/eth/account/0xadBd0E1a569ce5AE35951540a4FEc34c4BA14f44`, {
      method: 'GET',
      headers: {
        'Access-Control-Allow-Origin': '*',
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
    })
    const account = await res.json()
    //   {
    //     "pubKey": "f9c7cc2dc997954fbdc700bf09b1aaf596ecabedd5bab786207b041bea0eeb7e18d2e7818263d90dc3cd900e71031df68ef135e65f68cd2336aaf8f6b9084e1e",
    //     "priceInUsd": 175.47334000833334,
    //     "numberTxsSent": 9,
    //     "isContract": false,
    //     "balance": "225372549583425584",
    //     "address": "adbd0e1a569ce5ae35951540a4fec34c4ba14f44"
    // }
    dispatch({
      type: ActionTypes.TRANSACTIONLIST_FETCHITEMS_SUCCEED,
      items: [{
        title: '"Divide" Album by Ed Sheeran (Quantity: 1)',
        eth: '0.008291873964',
        tx: '0x0407f7b7e4a4f1d16e0bbe27133893913eb1a9623cc5fc92d41db408120201c2',
      }],
      account,
    })
  } catch (errors) {
    dispatch({
      type: ActionTypes.TRANSACTIONLIST_FETCHITEMS_FAIL,
      fetchItemsErrors: errors,
    })
  }
}

export const changeTable = params => (dispatch) => {
  const { pagination = {}, filters = {}, sorter = {} } = params
  dispatch({
    type: ActionTypes.TRANSACTIONLIST_CHANGETABLE_CHANGE,
    pagination,
    filters,
    sorter,
  })
}

export const searchTable = ({ isSearching }) => (dispatch) => {
  dispatch({
    type: ActionTypes.TRANSACTIONLIST_SEARCHTABLE_SEARCH,
    isSearching,
  })
}

export const editSearch = params => (dispatch) => {
  const { search } = params
  dispatch({
    type: ActionTypes.TRANSACTIONLIST_SEARCHTABLE_EDIT,
    search,
  })
}
